﻿using Common.Jwt;
using Interface;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Model.Entitys;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace Service
{
    public class CustomHSJWTService : ICustomJWTService
    {
        private readonly JWTTokenOptions _JWTTokenOptions;
        public CustomHSJWTService(IOptionsMonitor<JWTTokenOptions> jwtTokenOptions)
        {
            _JWTTokenOptions = jwtTokenOptions.CurrentValue;
        }
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<string> GetTokenAsync(Employee employee)
        {
            var result = await Task.Run(() =>
            {
                #region 避免敏感信息
                var claims = new[]
                {
                    new Claim("Id",employee.Id.ToString()),
                    new Claim("Name",employee.Name),
                    new Claim("Username",employee.Username),
                    new Claim("Phone",employee.Phone)
                };
                //需要加密：需要加密key:
                //Nuget引入：Microsoft.IdentityModel.Tokens
                SymmetricSecurityKey key = new (Encoding.UTF8.GetBytes(_JWTTokenOptions.SecurityKey));

                SigningCredentials creds = new (key, SecurityAlgorithms.HmacSha256);

                //Nuget引入：System.IdentityModel.Tokens.Jwt
                JwtSecurityToken token = new (
                 issuer: _JWTTokenOptions.Issuer,
                 audience: _JWTTokenOptions.Audience,
                 claims: claims,
                 expires: DateTime.Now.AddMinutes(20),//20分钟有效期
                 signingCredentials: creds);

                string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
                return returnToken;
            });
            #endregion
            return result;
        }

        public async Task<string> GetTokenAsync(User user)
        {
            var result = await Task.Run(() =>
            {
                #region 避免敏感信息
                var claims = new[]
                {
                    new Claim("Id",user.Id.ToString()),
                    new Claim("Name",user.Name),
                    new Claim("Phone",user.Phone)
                };
                //需要加密：需要加密key:
                //Nuget引入：Microsoft.IdentityModel.Tokens
                SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_JWTTokenOptions.SecurityKey));

                SigningCredentials creds = new(key, SecurityAlgorithms.HmacSha256);

                //Nuget引入：System.IdentityModel.Tokens.Jwt
                JwtSecurityToken token = new(
                 issuer: _JWTTokenOptions.Issuer,
                 audience: _JWTTokenOptions.Audience,
                 claims: claims,
                 expires: DateTime.Now.AddMinutes(20),//20分钟有效期
                 signingCredentials: creds);

                string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
                return returnToken;
            });
            #endregion
            return result;
        }
    }
}
